package com.gxa.autoparts.portal.controller;

import com.gxa.autoparts.core.common.JsonResult;
import com.gxa.autoparts.core.pojo.Address;
import com.gxa.autoparts.portal.service.AddressService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * @author R
 * @data 2022-01-06 15:29
 */
@RestController
@RequestMapping("/address")
@Api("地址管理")
public class AddressController {

    @Autowired
    AddressService service;

    @PostMapping("/add")
    @ApiOperation("新增地址")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "username",value = "收货人姓名",paramType = "query",dataType = "String",required = true),
            @ApiImplicitParam(name = "phone",value = "收货人联系电话",paramType = "query",dataType = "String",required = true),
            @ApiImplicitParam(name = "location",value = "地址",paramType = "query",dataType = "String",required = true),
            @ApiImplicitParam(name = "address",value = "详细地址",paramType = "query",dataType = "String",required = true),
            @ApiImplicitParam(name = "isDefault",value = "是否设置为默认地址",paramType = "query",dataType = "Boolean",required = true)
    })
    public Map addAddress(Long uid,String username,String phone,String location,String address,Boolean isDefault){
        Address address1 = new Address();
        address1.setUserId(uid);
        address1.setAddress(address);
        address1.setLocation(location);
        address1.setPhone(phone);
        address1.setUserName(username);
        address1.setIsDefault(isDefault);
        // 查看此时该用户是否有默认地址
        Address address2 = service.selectByDefault(uid);
        if (isDefault){
            // 如果此时isDefault为true，则表示该用户要把改地址设置为默认地址
            if (address2 != null){
                // 表示此时用户有默认地址
                // 先把此时的默认地址改为非默认地址
                address2.setIsDefault(false);
                service.updateById(address2);
            }
        }
        service.insert(address1);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setMsg("添加成功");
        jsonResult.setCode("200");
        return jsonResult.getDataMap();

    }

    @PostMapping("/selectAddresses")
    @ApiOperation("查询所有地址")
    public Map selectByUid(Long uid){
        List<Address> addresses = service.selectByUid(uid);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setData(addresses);
        jsonResult.setCode("200");
        jsonResult.setMsg("查询成功");
        return jsonResult.getDataMap();
    }

    @PostMapping("/updateAddresses")
    @ApiOperation("修改地址")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "收货地址id",paramType = "query",dataType = "Long",required = true),
            @ApiImplicitParam(name = "username",value = "收货人姓名",paramType = "query",dataType = "String",required = true),
            @ApiImplicitParam(name = "phone",value = "收货人联系电话",paramType = "query",dataType = "String",required = true),
            @ApiImplicitParam(name = "location",value = "地址",paramType = "query",dataType = "String",required = true),
            @ApiImplicitParam(name = "address",value = "详细地址",paramType = "query",dataType = "String",required = true),
            @ApiImplicitParam(name = "isDefault",value = "是否设置为默认地址",paramType = "query",dataType = "Boolean",required = true)
    })
    public Map updateAddress(Long uid,Long id,String username,String phone,String location,String address,Boolean isDefault){
        System.out.println("isDefault:----------------------------------"+isDefault);

        Address address1 = new Address();
        address1.setId(id);
        address1.setUserId(uid);
        address1.setAddress(address);
        address1.setLocation(location);
        address1.setPhone(phone);
        address1.setUserName(username);
        address1.setIsDefault(isDefault);
        // 查看此时该用户是否有默认地址
        Address address2 = service.selectByDefault(uid);
        if (isDefault){
            // 如果此时isDefault为true，则表示该用户要把改地址设置为默认地址
            if (address2 != null){
                // 表示此时用户有默认地址
                // 先把此时的默认地址改为非默认地址
                address2.setIsDefault(false);
                service.updateById(address2);
            }
        }
        service.updateById(address1);
        JsonResult jsonResult = new JsonResult();
//        jsonResult.setData(addresses);
        jsonResult.setCode("200");
        jsonResult.setMsg("修改成功");
        return jsonResult.getDataMap();
    }

    @PostMapping("/delete")
    @ApiOperation("删除地址")
    @ApiImplicitParam(name = "id",value = "需要删除的地址id",paramType = "query",dataType = "Long",required = true)
    public Map delete(Long id){
        Integer integer = service.deleteById(id);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setMsg("删除成功");
        jsonResult.setCode("200");
        return jsonResult.getDataMap();
    }

    @PostMapping("/selectById")
    @ApiOperation("查询单个地址详情信息")
    @ApiImplicitParam(name = "id",value = "地址Id",required = true,paramType = "query",dataType = "Long")
    public Map selectById(Long userId,Long id){
        Address address = service.selectById(userId, id);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("查询成功");
        jsonResult.setData(address);
        return jsonResult.getDataMap();
    }

}
